package de.gbeine.java.depview.beans;

import java.util.Set;

import lombok.extern.slf4j.Slf4j;

import com.google.common.eventbus.EventBus;

/**
 * An EventDispatcher which coordinates the subscribers to an EventBus.
 *
 * Every EventDispatcher need an EventBus.
 * The EventBus can be shared between multiple EventDispatchers.
 */
@Slf4j
public final class EventDispatcher {

    /**
     * Instance of EventBus to subscribe to.
     */
    private EventBus eventBus;

    /**
     * Creates an EvenDispatcher for a specified EventBus.
     * @param pEventBus The EventBus to subscribe to.
     */
    public EventDispatcher(final EventBus pEventBus) {
        super();
        this.eventBus = pEventBus;
    }

    /**
     * Add a collection of subscribers to an EventBus.
     * @param subscriber The objects to subscribe to the EventBus.
     */
    public void setSubscriber(final Set<Object> subscriber) {
        for (Object object : subscriber) {
            EventDispatcher.log.debug("Add subscriber to event bus: {}", object);
            this.eventBus.register(object);
        }
    }
}
